package cn.yinzhiping.redislearn.task;

import cn.yinzhiping.redislearn.domain.User;
import cn.yinzhiping.redislearn.framework.DistributedLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 模拟测试分布式锁
 * @author yinzhiping
 */
@Component
public class TestLockSchedule {
    private final static String LOCK_ID = "happy-to-learn-java";

    @Autowired
    DistributedLock distributedLock;

    //@Scheduled(cron = "0/10 * * * * ? ")
    public void doSomething() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class<?> cls = Class.forName("cn.yinzhiping.redislearn.domain.User");
        User user = (User) cls.newInstance();
        user.setAge(11);
        System.out.println(user);
        boolean lock = distributedLock.getLock(LOCK_ID, 10 * 1000);
        if (lock) {
            System.out.println("执行任务");
            distributedLock.releaseLock(LOCK_ID);
        } else {
            System.out.println("没有抢到锁");
        }
    }
}
